package com.example.demo.controller;

import com.example.demo.service.SelectByGoodsImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

@Controller
public class CommonController {

    private static final int OFFSET = 2;//分页查询偏移量(页面展示数量)

    @Autowired
    private SelectByGoodsImpl selectByGoods;

    @RequestMapping("/")
    public String toIndex(HttpServletRequest request, ModelMap modelMap, Integer index) {//index为页码
        String msg = "请登录";
        if (index == null) {
            index = 0;
            modelMap.put("nowPage", 1);
        } else {
            modelMap.put("nowPage", index);
            index = index - 1;
        }
        if (request.getSession().getAttribute("userName") == null) {
            request.getSession().setAttribute("userName", msg);
            request.getSession().setAttribute("loginUrl", "/User/Login");
        }
        Integer allCount = selectByGoods.selectAllCount();
        Integer pageCount = null;
        if (allCount % OFFSET == 0) pageCount = allCount / OFFSET;
        else pageCount = allCount / OFFSET + 1;
        modelMap.put("flag", 0);
        modelMap.put("pages", pageCount);
        modelMap.put("products", selectByGoods.selectLimit(index, OFFSET));
        return "index";
    }

    @RequestMapping("/index/kind")
    @ResponseBody
    public ModelAndView indexData(String kind, Integer index, HttpServletRequest request) {
        if (request.getSession().getAttribute("kind") == null) {
            request.getSession().setAttribute("kind", kind);
        }
        if (kind == null) {
            kind = (String) request.getSession().getAttribute("kind");
        }
        ModelAndView mav = new ModelAndView();
        if (index == null) {
            index = 0;
            mav.addObject("nowPage", 1);
        } else {
            mav.addObject("nowPage", index);
            index = index - 1;
        }
        mav.addObject("flag", -1);
        Integer allCount = selectByGoods.selectCountByKind(kind);
        Integer pageCount = null;
        if (allCount % OFFSET == 0) pageCount = allCount / OFFSET;
        else pageCount = allCount / OFFSET + 1;
        mav.addObject("pages", pageCount);
        mav.addObject("products", selectByGoods.selectByKindAndLimit(kind, index, OFFSET));
        mav.setViewName("index");
        return mav;
    }

    @RequestMapping("/index/search")
    @ResponseBody
    public ModelAndView indexSearch(String search, Integer index, HttpServletRequest request) {
        if (request.getSession().getAttribute("search") == null) {
            request.getSession().setAttribute("search", search);
        }
        if (search == null) {
            search = (String) request.getSession().getAttribute("search");
        }
        ModelAndView view = new ModelAndView();
        if (index == null) {
            index = 0;
            view.addObject("nowPage", 1);
        } else {
            view.addObject("nowPage", index);
            index = index - 1;
        }
        view.addObject("flag", 1);
        Integer allCount = selectByGoods.selectCountByGoodsNameLike(search);
        Integer pageCount = null;
        if (allCount % OFFSET == 0) pageCount = allCount / OFFSET;
        else pageCount = allCount / OFFSET + 1;
        view.addObject("pages", pageCount);
        view.addObject("products", selectByGoods.selectByLikeByGoodsNameAndLimit(search, index, OFFSET));
        view.setViewName("index");
        return view;
    }

    @RequestMapping("/detail")
    public String toDetail(Long id, ModelMap modelMap) {
        if (id == null) return "404";
        modelMap.put("product", selectByGoods.selectById(id));
        return "detail";
    }

    @RequestMapping("/test")
    public String toTest() {
        return "test";
    }

    @RequestMapping("/404")
    public String toCuoWu() {
        return "404";
    }

    @RequestMapping("/exit")
    @ResponseBody
    public String toExit(HttpServletRequest request) {
        request.getSession().setAttribute("userName", "请登录");
        request.getSession().setAttribute("loginUrl", "/User/Login");
        return "yes";
    }

    @RequestMapping("/getSessionOfUserName/Data")
    @ResponseBody
    public String getSessionOfUserName(HttpServletRequest request) {
        return (String) request.getSession().getAttribute("userName");
    }
}
